﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using PTiRouZ.Net.SilverLight.Api.HelloTipi;
using System.Collections.ObjectModel;
using PTiRouZ.Net.SilverLight.Api;
using Seesmic.Sdp.Extensibility;
using System.ComponentModel;
using Seesmic.Sdp.Utils;
using System.Collections.Generic;
using System.Collections;
using System.Windows.Threading;

namespace Seesmic.HelloTipi
{
    public class PhotosControlModel<T> : INotifyPropertyChanged where T : photo
    {
        public RelayCommand DownloadPhotoCommand { get; private set; }
        public RelayCommand DownloadAlbumCommand { get; private set; }
        public RelayCommand AddtoFavoriteCommand { get; private set; }
        public RelayCommand CommentPhotoCommand { get; private set; }
        public RelayCommand UploadPhotoCommand { get; private set; }
        
        /*public ICommand DownloadPhotoCommand
        {
            get
            {
                return new RelayCommand("Télécharger la photo", this.OnDownloadPhotoExecuted, this.CanDownloadPhoto);
            }
        }
        public ICommand DownloadAlbumCommand
        {
            get
            {
                return new RelayCommand("Télécharger l'album", this.OnDownloadAlbumExecuted, this.CanDownloadAlbum);
            }
        }
        public ICommand AddtoFavoriteCommand
        {
            get
            {
                return new RelayCommand("Ajouter aux favoris", this.OnAddtoFavoriteExecuted, this.CanAddtoFavorite);
            }
        }
        public ICommand CommentPhotoCommand
        {
            get
            {
                return new RelayCommand("Commenter la photo", this.OnCommentPhotoExecuted, this.CanCommentPhoto);
            }
        }
        public ICommand UploadPhotoCommand
        {
            get
            {
                return new RelayCommand("Uploader une photo", this.OnUploadPhotoExecuted, this.CanUploadPhoto);
            }
        }*/

        protected HelloTipiAccount _account;
        protected apitxt api;
        private bool _family;
        public bool family
        {
            get { return _family; }
            set { _family = value; OnPropertyChanged("family"); }
        }

        private string _comment;
        public string Comment
        {
            get { return _comment; }
            set { _comment = value; OnPropertyChanged("Comment"); }
        }

        protected ObservableCollection<T> _photosCollection;
        public ObservableCollection<T> PhotosCollection
        {
            get { return _photosCollection; }
            set
            {
                _photosCollection = value;
                OnPropertyChanged("PhotosCollection");
                /*IEnumerator<T> Enumerator = value.GetEnumerator();
                System.Threading.Thread.Sleep(1000);
                PhotoSelected = Enumerator.MoveNext() as T;*/
            }
        }

        protected T _photoSelected;
        public T PhotoSelected
        {
            get { return _photoSelected; }
            set { _photoSelected = value; OnPropertyChanged("PhotoSelected"); }
        }

        public PhotosControlModel(HelloTipiAccount Account)
        {

            this._account = Account;
            this.api = new apitxt(_account.Username, _account.Password, "Seesmic Desktop 2");
            HelloTipiPlugin.LogInfo("Creation de l'api avec " + this._account.ToString());

            this.api.APICallError += delegate(object sender, ApiErrorEventArgs e)
            {
                HelloTipiPlugin.ShowDialogNotification("Erreur de l'api", "{0}", DialogNotificationButtons.OK, e.Message);
            };

            _pbTraitement = new ProgressBar();
            _pbDownload = new ProgressBar();

            DownloadPhotoCommand = new RelayCommand("Télécharger la photo", this.OnDownloadPhotoExecuted, this.CanDownloadPhoto);
            DownloadAlbumCommand = new RelayCommand("Télécharger l'album", this.OnDownloadAlbumExecuted, this.CanDownloadAlbum);
            AddtoFavoriteCommand = new RelayCommand("Ajouter aux favoris", this.OnAddtoFavoriteExecuted, this.CanAddtoFavorite);
            CommentPhotoCommand = new RelayCommand("Commenter la photo", this.OnCommentPhotoExecuted, this.CanCommentPhoto);
            UploadPhotoCommand = new RelayCommand("Uploader une photo", this.OnUploadPhotoExecuted, this.CanUploadPhoto);
        }

        private ProgressBar _pbTraitement;

        public ProgressBar pbTraitement
        {
            get { return _pbTraitement; }
            set { _pbTraitement = value; OnPropertyChanged("pbTraitement"); }
        }

        private ProgressBar _pbDownload;

        public ProgressBar pbDownload
        {
            get { return _pbDownload; }
            set { _pbDownload = value; OnPropertyChanged("pbDownload"); }
        }

        #region Command

        private bool CanDownloadPhoto()
        {
            return true;
        }

        public void OnDownloadPhotoExecuted()
        {
            ArchivePhotos ap = new ArchivePhotos(_photoSelected);
            ExecuteArchiving(ap);
        }

        private bool CanDownloadAlbum()
        {
            return true;
        }

        public void OnDownloadAlbumExecuted()
        {
            List<photosUrl> ListPhotos = new List<photosUrl>();
            foreach (T photo in PhotosCollection)
            {
                ListPhotos.Add(new photosUrl(photo.id, photo.url_small, photo.url_medium, photo.url_large));
            }
            ArchivePhotos ap = new ArchivePhotos(ListPhotos);

            ExecuteArchiving(ap);
        }

        private bool CanAddtoFavorite()
        {
            return false;// family;
        }

        public void OnAddtoFavoriteExecuted()
        {

        }

        protected virtual bool CanCommentPhoto()
        {
            return family;
        }

        public virtual void OnCommentPhotoExecuted() { }

        protected virtual bool CanUploadPhoto()
        {
            return family;
        }

        public virtual void OnUploadPhotoExecuted() { }
        
        #endregion
     

        void AlbumDownloadCompleted(object sender, AlbumDownloadCompletedEventArgs e)
        {
            HelloTipiPlugin.ShowDialogNotification("Téléchargement effectué", "{0} à bien été enregistré", DialogNotificationButtons.OK, e.FileName);
        }

        void AlbumDownloadProgressChanged(object sender, TraitementProgressChangedEventArgs e)
        {
            pbTraitement.Minimum = 0;
            pbTraitement.Maximum = e.TotalFileToTraited;
            pbTraitement.Value = e.FileTraited;
        }

        void PhotoDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            pbDownload.Minimum = 0;
            pbDownload.Maximum = e.TotalBytesToReceive;
            pbDownload.Value = e.BytesReceived;
        }

        private void ExecuteArchiving(ArchivePhotos ap)
        {
            ap.PhotoDownloadProgressChanged += new EventHandler<DownloadProgressChangedEventArgs>(PhotoDownloadProgressChanged);
            ap.AlbumDownloadProgressChanged += new EventHandler<TraitementProgressChangedEventArgs>(AlbumDownloadProgressChanged);
            ap.AlbumDownloadCompleted += new EventHandler<AlbumDownloadCompletedEventArgs>(AlbumDownloadCompleted);
            bool res = ap.Execute();
        }        

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                if (name == "PhotosCollection")
                    (CommentPhotoCommand as RelayCommand).RaiseCanExecuteChanged();
                
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        #endregion
    }
}
